package com.food.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.food.dao.service.CollectService;
import com.food.dao.service.MessageService;
import com.food.model.Collect;
import com.food.model.Member;
import com.food.model.Message;

/**
 * Servlet implementation class MessageServlet
 */
@WebServlet("/recipe/MessageServlet")
public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		try{
			int rid = Integer.parseInt(request.getParameter("rid"));
			HttpSession session = request.getSession();
			Member member = (Member)session.getAttribute("user");
			String messageword = request.getParameter("messageword");
			if(messageword!=null && messageword.trim().length()>=1){
			Message message = new Message();
			message.setRid(rid);
			message.setMbid(member.getMbid());
			message.setMessageword(messageword);

			MessageService messageService = new MessageService();
			
			messageService.insertMessage(message);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("/recipe/ShowRecipe?rid="+rid);
			rd.forward(request, response);
			return;
			}
			
			
			
			}
			catch(Exception e){
				
				RequestDispatcher rd = request
						.getRequestDispatcher("/index.jsp");
				rd.forward(request, response);
				return;
			}
	}

}
